package com.changgou.order.mq.queue;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 延时队列
 */
@Configuration
public class QueueConfig {
    /**
     * 延时队列,过期会发送消息
     * true 序列化
     * @return
     */
    @Bean
    public Queue orderDelayQueue(){
        return QueueBuilder
                //队列名
                .durable("orderDelayQueue")
                //orderDelayQueue数据过期后会进入死信队列,需要绑定交换机
                .withArgument("x-dead-letter-exchange","orderListenerExchange")
                //死信数据的路由队列
                .withArgument("x-dead-letter-routing-key","orderListenerQueue")
                .build();
    }

    /**
     * 接受消息的队列
     * @return
     */
    @Bean
    public Queue orderListenerQueue(){
        return new Queue("orderListenerQueue",true);
    }
    /**
     * 交换机
     */
    @Bean
    public Exchange orderListenerExchange(){
        return new DirectExchange("orderListenerExchange");
    }
    /**
     * 绑定器
     */
    @Bean
    public Binding orderListenerBinding(Queue orderListenerQueue,Exchange orderListenerExchange){
        return BindingBuilder.bind(orderListenerQueue).to(orderListenerExchange).with("orderListenerQueue").noargs();
    }
}
